//
//  AdvertCell.m
//  wtshoppingcite
//
//  Created by TW on 15-3-18.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "AdvertCell.h"
#import "JVButton.h"

@implementation AdvertCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    [self.contentView addSubview:view];
    

    _seperateView = [[UIView alloc]initWithFrame:CGRectMake(2, view.height/2 - 0.5, kWidth-4, 1)];
    _seperateView.backgroundColor = MAIN_GRAY_COLOR;
    [view addSubview:_seperateView];
    
    _titleLabel = [self createLabelWithFrame:CGRectMake(kWidth/2-30,0, 60, 20) text:@"" font:14.0f textAli:NSTextAlignmentCenter textColor:MAIN_BLUE_COLOR];
    _titleLabel.backgroundColor = [UIColor whiteColor];
    [view addSubview:_titleLabel];
    
    _fucButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, view.bottom, SCREEN_WIDTH, 160)];
    _fucButtonView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_fucButtonView];
    
}

-(void)setButtonTitle:(NSArray*)titles images:(NSArray*)images{
    
    
//    NSArray* colors = @[@"tongzhi",@"jiao",@"huodon",@"xiu",@"dianxin",@"jiazheng",@"licai",@"health"];
//    NSArray* titles = @[@"银行",@"政府",@"街道办",@"派出所",@"法院",@"医院",@"药店",@"学校"];
    [[_fucButtonView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    float ff = 8;
    float ox = 10;
    float oy = 5;
    float fw = (SCREEN_WIDTH - 4*ox)/4;
    float fh = 70;
    for(int i = 0; i<8;i++){
        ox =(i%4*fw)+(i%4+1)*ff;
        if(i%4 == 0 && i!=0){
            oy = oy+fh+5;
        }
        JVButton *button = [JVButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(ox, oy, fw, fh);
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:COLOR_RGB(104, 104, 104) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        // UIImage* colorImg = [UIImage createImageWithBgColor:colors[i] size:CGSizeMake(20, 20) cornerRadius:20];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        button.tag = 300+i;
        [button addTarget:self action:@selector(pressClick:) forControlEvents:UIControlEventTouchUpInside];
        [_fucButtonView addSubview:button];
    }
}

-(void)pressClick:(JVButton*)button{
    NSInteger index = button.tag -300;
   // NSLog(@"点击了第%d个",index);
    
    if([self.advDelegate respondsToSelector:@selector(selectAdvertSetcionClickWithDataDic:withIndexTag:)]){
        [self.advDelegate selectAdvertSetcionClickWithDataDic:_dataDic withIndexTag:index];
    }
}

-(void)setTitle:(NSString *)title{
    
    if (_title != title) {
        _title = title;
        _titleLabel.text = title;
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    
    if(dataDic != _dataDic){
        _dataDic = dataDic;
        
        
        self.title = [dataDic objectForKey:@"title"];
        NSArray* titles = [dataDic objectForKey:@"names"];
        NSArray* images = [dataDic objectForKey:@"images"];
        [self setButtonTitle:titles images:images];

    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

@end
